<template>
  <div>
    el-form:表单组件标签 label-width:表单每一项标题的宽度
    el-form-item:表单内的每一项 label:该项的标题 表单每一项内再包含表单元素
    表单验证: 1.el-form上绑定model(表单的数据) 2.el-form上绑定rules(验证规则)
    3.在el-form-item上绑定prop(

    <!-- el-form组件内有一个validate方法,它是用于全局表单验证的,我们需要调用一下 -->
    <el-form :model="form" label-width="100px" :rules="rules" ref="aa">
      <el-form-item label="手机号" prop="tel">
        <el-input v-model="form.tel"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码框" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">确定</el-button>
      </el-form-item>
    </el-form>
    <Son ref="son"></Son>
    <button @click="btn">点阿基</button>
  </div>
</template>

<script>
import Son from './component/Son.vue'
export default {
  components: { Son },
  data () {
    return {
      form: {
        username: '',
        password: '',
        tel: ''
      },
      rules: {
        tel: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          //   {
          //     pattern: /0?(13|14|15|18|17)[0-9]{9}/,
          //     message: '请正确输入手机号',
          //     taigger: 'change'
          //   }
          {
            validator: (rule, value, callback) => {
              const reg = /0?(13|14|15|18|17)[0-9]{9}/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'blur'
          }
        ],
        username: [
          { required: true, message: '请输入活动名称', trigger: 'change' },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'change'
          },
          {
            validator: (rule, value, callback) => {
              value.startsWith('ab')
                ? callback()
                : callback(new Error('必须以ab开头'))
            },
            taigger: 'change'
          }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    btn () {
      this.$refs.son.validate()
    },
    submit () {
      // from表单的全局表单验证
      this.$refs.aa.validate(result => {
        console.log(result)
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
